package gqz.mvvm.view.custom;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Administrator on 2017/12/11.
 * PathMeasure的基本使用
 * ValueAnimator的基本使用
 */

public class MyPath2 extends View {

    String TAG = "MyPath---";

    enum State {Circle, Trangle, Finish}

    private State curState = State.Circle;
    private int width = 500, height = width;
    private Path drawPath, pathC1, pathC2, pathT1, pathT2;
    private Paint paint;
    private PathMeasure pathMeasure;
    private float pathLength1, pathLength2, pathLength3, pathLength4;
    private float value;//ValueAnimator计算出来的结果
    private ValueAnimator valueAnimator;

    public MyPath2(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStrokeJoin(Paint.Join.BEVEL);
        paint.setShadowLayer(10, 0, 0, Color.BLACK);//阴影
        paint.setStrokeWidth(10);

        drawPath = new Path();
        pathC1 = new Path();
        pathC2 = new Path();
        pathT1 = new Path();
        pathT2 = new Path();
        pathMeasure = new PathMeasure();

//        pathC1.addCircle(width / 2, height / 2, width / 2 - 10, Path.Direction.CW);
        pathC1.addArc(new RectF(10, 10, width - 10, width - 10), 90, -359.9f);//不能使用360，否则会找不到起点
        pathC2.addArc(new RectF(50, 50, width - 50, width - 50), 150, -359.9f);

        pathMeasure.setPath(pathC1, false);//将path和pathMeasure绑定
        pathLength1 = pathMeasure.getLength();//获取path的长度
        pathMeasure.setPath(pathC2, false);
        pathLength2 = pathMeasure.getLength();
        //三角形
        float[] pos = new float[2];
        pathMeasure.getPosTan(0, pos, null);
        pathT1.moveTo(pos[0], pos[1]);
        pathMeasure.getPosTan(1f / 3f * pathLength2, pos, null);
        pathT1.lineTo(pos[0], pos[1]);
        pathMeasure.getPosTan(2f / 3f * pathLength2, pos, null);
        pathT1.lineTo(pos[0], pos[1]);
        pathT1.close();

//        pathMeasure.getPosTan(2f / 3f * pathLength2, pos, null);
        Matrix matrix = new Matrix();
        matrix.postRotate(180, width / 2, width / 2);
        pathT1.transform(matrix, pathT2);//将pathT1进行矩阵变换，将结果保存到pathT2

        pathMeasure.setPath(pathT1, false);
        pathLength3 = pathMeasure.getLength();
        pathMeasure.setPath(pathT2, false);
        pathLength4 = pathMeasure.getLength();

        //ValueAnimator不直接实现动画，需要动过AnimatorUpdateListener来监听变化，将变化的属性值设置给对象，然后重绘以达到动画的效果
        valueAnimator = ValueAnimator.ofFloat(0, 1);//ofFloat()创建实例
        valueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                handler.sendEmptyMessage(0);
            }

            @Override
            public void onAnimationCancel(Animator animation) {
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
            }
        });
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
//                Log.v(TAG, "" + animation.getAnimatedValue());
                value = (float) animation.getAnimatedValue();//获取计算出的属性值
                invalidate();
            }
        });
        valueAnimator.setDuration(3000);
//        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.start();
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (curState) {
                case Circle:
                    curState = State.Trangle;
                    valueAnimator.start();
                    break;
                case Trangle:
                    curState = State.Finish;
                    valueAnimator.start();
                    break;
                case Finish:
                    curState = State.Circle;
                    valueAnimator.start();
                    break;
            }
        }
    };

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        switch (curState) {
            case Circle:
                drawPath.reset();
//        drawPath.lineTo(0,0);// 硬件加速的BUG
                pathMeasure.setPath(pathC1, false);
                float stop = pathLength1 * value;
                pathMeasure.getSegment(0, stop, drawPath, true);
                canvas.drawPath(drawPath, paint);

                drawPath.reset();
                pathMeasure.setPath(pathC2, false);
                stop = pathLength2 * value;
                pathMeasure.getSegment(0, stop, drawPath, true);
                canvas.drawPath(drawPath, paint);
                break;
            case Trangle:
                canvas.drawPath(pathC1, paint);
                canvas.drawPath(pathC2, paint);

                drawPath.reset();
                pathMeasure.setPath(pathT1, false);
                float stop2 = pathLength3 * value;
                float startD = stop2 - (0.5f - Math.abs(0.5f - value)) * 200;
                pathMeasure.getSegment(startD, stop2, drawPath, true);
                canvas.drawPath(drawPath, paint);

                drawPath.reset();
                pathMeasure.setPath(pathT2, false);
                stop2 = pathLength4 * value;
                pathMeasure.getSegment(startD, stop2, drawPath, true);
                canvas.drawPath(drawPath, paint);
                break;
            case Finish:
                canvas.drawPath(pathC1, paint);
                canvas.drawPath(pathC2, paint);

                drawPath.reset();
                pathMeasure.setPath(pathT1, false);
                float stop3 = pathLength3 * value;
                pathMeasure.getSegment(0, stop3, drawPath, true);
                canvas.drawPath(drawPath, paint);

                drawPath.reset();
                pathMeasure.setPath(pathT2, false);
                stop3 = pathLength4 * value;
                pathMeasure.getSegment(0, stop3, drawPath, true);
                canvas.drawPath(drawPath, paint);
                break;
        }
    }
}
